//
//  AppDelegate_iPad.m
//  WRY
//
//  Created by Yang on 8/19/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "AppDelegate_iPad.h"
#import "LoginVC_iPad.h"
#import "RootVC_iPad.h"
#import "CommonJson.h"
#import "WebPath.h"
#import "NSStringExtra.h"
@implementation AppDelegate_iPad

@synthesize window;
@synthesize loginController,rootController;
@synthesize GUID;
@synthesize userCantonCodes,showCantonName,userType;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
	
    //检测更新
//    [self cheackUpdate];
    
    
    
    [self addLoginVc];

//    NSLog(@"self.window.frame:%@",NSStringFromCGRect(self.window.frame));
//    NSLog(@"[[UIScreen mainScreen]bounds]:%@",NSStringFromCGRect([[UIScreen mainScreen]bounds]));

    
    [window makeKeyAndVisible];
	
	return YES;
}

+ (AppDelegate_iPad *)currentDelegate {
	return (AppDelegate_iPad *)[[UIApplication sharedApplication] delegate];
}
-(void)setWindowRootController:(UIViewController *)controller
{
//    if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
//        
//    { // warning: addSubView doesn't work on iOS6
//        
//        [window addSubview:controller.view];
//    }
//    else
//        
//    { // use this mehod on ios6
//        
//        [window setRootViewController:controller];
//    }
    
    [window setRootViewController:controller];
}
-(void)addLoginVc
{
    LoginVC_iPad *loginVc=[[LoginVC_iPad alloc]initWithNibName:@"LoginVC_iPad" bundle:nil];
    self.loginController=loginVc;
    [loginVc release];
    [self setWindowRootController:self.loginController];
	
}
-(void)addRootVc
{
    
    RootVC_iPad *rootViewController = [[RootVC_iPad alloc] init];//WithNibName:@"RootVC_iPad" bundle:nil];
    self.rootController=rootViewController;
    [rootViewController release];
    [self setWindowRootController:self.rootController];
    
}
-(void)restartApplication
{
    [self addLoginVc];
}
- (void)applicationWillResignActive:(UIApplication *)application {
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive.
     */
}


- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate.
     */
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     */
    NSString *loginUserName=[[NSUserDefaults standardUserDefaults]objectForKey:@"LoginName"];
    NSString *loginUserPwd=[[NSUserDefaults standardUserDefaults]objectForKey:@"LoginPwd"];
    NSString *deviceType=[[NSUserDefaults standardUserDefaults]objectForKey:@"DeviceType"];
    NSString *deviceOnlyCode=[[NSUserDefaults standardUserDefaults]objectForKey:@"DeviceOnlyCode"];
    if(![loginUserName isEmptyOrNull]  && ![deviceType isEmptyOrNull] && ![deviceOnlyCode isEmptyOrNull] )
    {
        //自动登录
        NSString *loginBody=[NSString stringWithFormat:@"UserName=%@&Password=%@&DeviceTypeID=%@&DeviceOnlyCode=%@",loginUserName,loginUserPwd,deviceType,deviceOnlyCode];
        [[CommonJson sharedInstance]requestToPath:[WebPath loginPath] methodName:@"Login" bodyString:loginBody targetObj:self targetMethodName:@"eventForLogin:" notifiName:@"requestLogin"];
    }
    
    
    
}
-(void)eventForLogin:(NSNotification *)notification
{
    [[CommonJson sharedInstance] removeTarget:self notiName:[notification name]];
	NSObject *object  = [notification object];
	if ([object isKindOfClass:[NSDictionary class]]) {
		NSDictionary *dict = (NSDictionary *)object;
		if ([[dict objectForKey:@"Status"] intValue]==1) {
			NSString *loginGuid=[NSString stringWithFormat:@"%@",[dict objectForKey:@"Data"]];
            if ([loginGuid length]>=36) {
                //保存loginguid
                self.GUID=loginGuid;
                //设置serviceID
                NSString *systemId=[[NSUserDefaults standardUserDefaults]objectForKey:@"SystemID"];
                if (![systemId isEmptyOrNull]) {
                    NSString *body=[NSString stringWithFormat:@"LoginGuid=%@&SystemID=%@",loginGuid,systemId];
                    [[CommonJson sharedInstance]requestToPath:[WebPath loginPath] methodName:@"SetServiceSource" bodyString:body targetObj:self targetMethodName:@"eventForServiceSource:" notifiName:@"requestServiceSource"];
                    return;
                }
                
            }
        }
    }
    //跳到登录窗体
    [self addLoginVc];
}
-(void)eventForServiceSource:(NSNotification *)notification
{
    [[CommonJson sharedInstance] removeTarget:self notiName:[notification name]];
//	
//	NSObject *object  = [notification object];
//	if ([object isKindOfClass:[NSDictionary class]]) {
//		NSDictionary *dict = (NSDictionary *)object;
//		if ([[dict objectForKey:@"Status"] intValue]==1) {
////            [self cheackUpdate];
//        }
//    }
}
-(void)cheackUpdate
{
//    [NSThread detachNewThreadSelector:@selector(requestVersion) toTarget:self withObject:nil];

    
}
-(void)requestVersion
{
//    NSString *versionUrl=@"http://58.210.204.106:60001/ipa/ipad/version.plist";
//    NSURL *url=[[NSURL alloc]initWithString:versionUrl];
//    NSDictionary *d=[[NSDictionary alloc]initWithContentsOfURL:url];
//    NSString *version=[d objectForKey:@"version"];
//    NSString *bundleVersion=[NSString stringWithFormat:@"%@",[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]];
//    if (![bundleVersion isEqualToString:version])
//    {
//        //@"itms-services://?action=download-manifest&url=http://10.15.208.70/app/nmgSystem.plist"
//        NSURL *safiUrl=[[NSURL alloc]initWithString:@"itms-services://?action=download-manifest&url=http://58.210.204.106:60001/ipa/ipad/wryipad.plist"];
//        [[UIApplication sharedApplication]openURL:safiUrl];
//    }
}
#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    /*
     Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
     */
}


- (void)dealloc {
    [super dealloc];
    [userType release];
    [showCantonName release];
    [userCantonCodes release];
	[GUID release];
	[loginController release];
    [rootController release];
    [window release];
}


@end
